package edu.rit.varapp.graphical;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;

/**
 * This abstract class is the parent of all views for the VAR application. It will center the frame, as well as
 * holds information for closing the application (or just disposing the frame).
 * @author Eric Kisner
 */
public class AbstractView extends JFrame {

    public AbstractView( Dimension dim ) {
        setLocation( dim );
    }

    /**
     * Closes the application, or just disposes the frame.
     * @param closeApp If true, the application will be closed, else the frame will just be disposed.
     */
    public void close( boolean closeApp ) {
        if( closeApp ) {
            System.exit( 0 );
        }
        dispose();
    }

    /**
     * Center the view according to the dimensions of the frame and the dimension of the user's screen.
     * @param dim The dimension of the current frame.
     */
    private void setLocation( Dimension dim ) {
        double width = dim.getWidth();
        double height = dim.getHeight();
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        double x = env.getCenterPoint().getX();
        double y = env.getCenterPoint().getY();

        setBounds( (int) (x - (width / 2)),
                   (int) (y - (height / 2)),
                   (int) width,
                   (int) height );
    }
}
